package cn.shadow.module.chess.message.chnChess.frame;

import cn.shadow.module.chess.message.chnChess.Camp;
import cn.shadow.module.chess.message.chnChess.ChessType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

import java.util.HashMap;
import java.util.Map;

public class ChessFactory {

   static Map<Camp, Map<State, Map<ChessType, TextureRegion>>> mapMap = new HashMap<>();


    static{
        load();
    }
    public static void load() {
        load("chess/卒", ChessType.BING, Camp.BLACK);
        load("chess/黑炮", ChessType.PAO, Camp.BLACK);
        load("chess/黑车", ChessType.CHE, Camp.BLACK);
        load("chess/黑马", ChessType.MA, Camp.BLACK);
        load("chess/象", ChessType.XIANG, Camp.BLACK);
        load("chess/黑士", ChessType.SHI, Camp.BLACK);
        load("chess/将", ChessType.SHUAI, Camp.BLACK);
        load("chess/兵", ChessType.BING, Camp.RED);
        load("chess/红炮", ChessType.PAO, Camp.RED);
        load("chess/车", ChessType.CHE, Camp.RED);
        load("chess/红马", ChessType.MA, Camp.RED);
        load("chess/相", ChessType.XIANG, Camp.RED);
        load("chess/红仕", ChessType.SHI, Camp.RED);
        load("chess/帅", ChessType.SHUAI, Camp.RED);
    }

    public static void load(String path, ChessType type, Camp camp) {
        State[] values = State.values();
        for (State state : values) {
            String temp = path;
            switch (state) {
                case CHECK:
                    temp += "_jiang";
                    break;
                case CHOSEN:
                    temp += "_on";
                    break;
                default:
                    break;
            }
            temp+=".png";
            FileHandle fileHandle = Gdx.files.internal(temp);
            Texture texture = new Texture(fileHandle);
            TextureRegion textureRegion = new TextureRegion(texture);
            Map<State, Map<ChessType, TextureRegion>> stateMap = mapMap.computeIfAbsent(camp, k -> new HashMap<>());
            Map<ChessType, TextureRegion> regionMap = stateMap.computeIfAbsent(state, k -> new HashMap<>());
            regionMap.putIfAbsent(type, textureRegion);
        }
    }

    public static ChessActor create(ChessType chessType, Camp camp) {
        return new ChessActor(chessType, camp, get(camp, chessType, State.NORMAL));
    }

    public static TextureRegion get(Camp camp, ChessType type, State state) {
        return mapMap.get(camp).get(state).get(type);
    }
}
